﻿Public Class AddressBox : Inherits UserControl

	Sub New()
		BackColor = Color.Transparent
		DoubleBuffered = True
	End Sub

	Dim _Menu As MenuItem
	Property Menu As MenuItem
		Get
			Return _Menu
		End Get
		Set(value As MenuItem)
			_Menu = value
			Controls.Clear()
			If value Is Nothing Then Return
			ToolStrip = New ToolStrip
			ToolStrip.BackColor = Color.White
			ToolStrip.Dock = DockStyle.None
			ToolStrip.AutoSize = False
			ToolStrip.Location = New Point(Height, 3)
			ToolStrip.Size = New Size(Width - 55, 20)
			ToolStrip.Padding = Padding.Empty
			ToolStrip.Margin = Padding.Empty
			ToolStrip.ImageScalingSize = New Size(16, 16)
			ToolStrip.Renderer = New AddressRenderer
			Controls.Add(ToolStrip)
			AddMenu(Menu)
			If ToolStrip.Items.Count = 0 Then Return
			ToolStrip.Width = ToolStrip.Items(ToolStrip.Items.Count - 1).Bounds.Right + 4
		End Set
	End Property

	Dim _Page As BrowserPage
	Property Page As BrowserPage
		Get
			Return _Page
		End Get
		Set(value As BrowserPage)
			_Page = value
			If Not value Is Nothing Then Menu = Nothing
			Invalidate()
		End Set
	End Property


	Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, keyData As System.Windows.Forms.Keys) As Boolean
		If Not TextBox Is Nothing Then
			If keyData = Keys.Escape Then
				TextBox.Visible = False
				ToolStrip.Visible = True
				Window.ActivePage.Focus()
				Return True
			ElseIf keyData = Keys.Enter Then
				Window.Launch(TextBox.Text)
				Return True
			End If
		End If
		Return MyBase.ProcessCmdKey(msg, keyData)
	End Function

	Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
		Dim R = ClientRectangle
		e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
		e.Graphics.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
		R.Inflate(-1, -1)
		e.Graphics.FillRoundRect(R, Color.Black.Opacity(96).Brush, 5)
		R.Inflate(-1, -1)
		e.Graphics.FillRoundRect(R, Brushes.White, 4)
		R = ClientRectangle
		R.Width = R.Height
		R.Inflate(-1, -1)
		R.Inflate(-1, -1)
		e.Graphics.FillRoundRect(R, Brushes.YellowGreen, 4, 0, 0, 4)
		e.Graphics.FillRoundRect(R, LGBv(R, Color.White.Opacity(224), Color.White.Opacity(192)), 4, 0, 0, 4)
		e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.None
		e.Graphics.PixelOffsetMode = Drawing2D.PixelOffsetMode.None
		e.Graphics.DrawLine(Color.Black.Opacity(128).Pen, R.Right, R.Top, R.Right, R.Bottom)
		R.Inflate(-3, -3)
		e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBilinear
		If Not Menu Is Nothing Then
			If Menu.Image Is Nothing Then Return
			e.Graphics.DrawImage(Menu.Image, R)
		ElseIf Not Page Is Nothing Then
			e.Graphics.DrawImage(Page.Image, R)
			R = New Rectangle(Height + 2, 0, Width - Height - 2, Height)
			DrawText2(e.Graphics, Page.Address, Font, Color.Black, R, StrFormat("lm"))
		End If
	End Sub

	Dim WithEvents TextBox As TextBox
	Dim WithEvents ToolStrip As ToolStrip

	Protected Overrides Sub OnMouseDown(e As System.Windows.Forms.MouseEventArgs)
		MyBase.OnMouseDown(e)
		Edit()
	End Sub

	Private Sub TextBox_LostFocus(sender As Object, e As System.EventArgs) Handles TextBox.LostFocus
		Controls.Remove(TextBox)
		TextBox = Nothing
	End Sub

	Private Sub AddMenu(MenuItem As MenuItem)
		If MenuItem.Parent Is Nothing Then
			Dim Home = ToolStrip.AddButton(MenuItem.Text, MenuItem.Text, Nothing)
			Home.Tag = MenuItem
			AddHandler Home.Click, AddressOf MenuClick
			Home.Padding = Padding.Empty
			Home.Margin = Padding.Empty
			Return
		End If
		AddMenu(MenuItem.Parent)
		Dim Item = MenuItem.Text
		Dim Button = ToolStrip.AddButton("^" & Item, Item, Nothing)
		Button.Tag = MenuItem
		AddHandler CType(Button, ToolStripSplitButton).ButtonClick, AddressOf MenuClick
		For Each SubMenu In MenuItem.Parent.Items
			If SubMenu.Text.StartsWith("""") Or SubMenu.Text.StartsWith("-") Then
				Dim Menu = ToolStrip.AddButtonMenu(Item, SubMenu.Name, "-", Nothing)
			Else
				Dim Menu = ToolStrip.AddButtonMenu(Item, SubMenu.Name, SubMenu.Text, SubMenu.Image)
				'FillMenu(SubMenu, Menu)
				Menu.Tag = SubMenu
				AddHandler Menu.Click, AddressOf MenuClick
			End If
		Next
		ToolStrip.Items(Item).Padding = Padding.Empty
		ToolStrip.Items(Item).Margin = Padding.Empty
	End Sub

	Private Sub MenuClick(sender As Object, e As EventArgs)
		CType(sender.tag, MenuItem).Launch(Window)
	End Sub

	Private Sub FillMenu(Parent As MenuItem, ParentMenu As ToolStripMenuItem)
		For Each SubMenu In Parent.Items
			If SubMenu.Text.StartsWith("""") Or SubMenu.Text.StartsWith("-") Then
				ParentMenu.DropDownItems.Add("-")
			Else
				Dim Menu = ParentMenu.DropDownItems.Add(SubMenu.Text, SubMenu.Image)
				Menu.Tag = SubMenu
				FillMenu(SubMenu, Menu)
				If Not SubMenu.HasItems Then AddHandler Menu.Click, AddressOf MenuClick
			End If
		Next
	End Sub

	ReadOnly Property Window As Window
		Get
			Dim p As Object = Parent
			Do Until TypeOf p Is Window
				p = p.Parent
			Loop
			Return p
		End Get
	End Property

	Sub Edit()
		TextBox = New TextBox
		TextBox.UseEnterAsTab = False
		TextBox.BackColor = Color.White
		TextBox.Location = New Point(Height + 2, 4)
		TextBox.Size = New Size(Width - 30, TextBox.Height)
		TextBox.Anchor = AnchorStyles.Left + AnchorStyles.Right + AnchorStyles.Top + AnchorStyles.Bottom
		Controls.Add(TextBox)
		TextBox.Text = Text
		TextBox.SelectAll()
		TextBox.Focus()
		ToolStrip.Visible = False
	End Sub

End Class